package com.codeoss.framework.view.pagebean.mobile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.util.ValueNamePair;
import com.codeoss.framework.view.clientscript.NavigatePageClientScript;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.TreePath;
import com.codeoss.framework.view.component.mobile.MListView;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.datasource.IDataSource;
import com.codeoss.framework.view.datasource.store.IDataSourceStore;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.framework.view.util.DataSourceUtil;
import com.codeoss.framework.view.util.PageBeanUtil;

@Service("mTreeListViewPageBean")
public class MTreeListViewPageBean extends  MListViewPageBean{
	@Autowired
	private IDataSourceStore dataSourceStore;
	@Autowired
	private IVariableService variableService;
	@Autowired
	private ITableStore tableStore;
	public static final String KEY_TREEPATH = "key_treepath";
	@Autowired
	private PageBeanUtil pageBeanUtil;
	@Autowired
	private DataSourceUtil dataSourceUtil;
	
	public void beforeInit() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		if(!page.isInit()) {
			pageBeanUtil.getComponent(yunContext, page, "mlistview");
			MListView mListView=(MListView)yunContext.getAttribute("__component__");
			page.setAttribute(KEY_MLISTVIEW, mListView);
			
			pageBeanUtil.getComponent(yunContext, page, "treepath");
			TreePath treePath=(TreePath)yunContext.getAttribute("__component__");
			treePath.setVarName("_treepath_");
			page.setAttribute(KEY_TREEPATH, treePath);
			
			List<IDataBinding> fieldList = new ArrayList<IDataBinding>();
			pageBeanUtil.getDataBindingList(mListView, fieldList);
			page.setAttribute(KEY_MLISTVIEW_FIELD_LIST, fieldList);
			
			page.setInit(true);
		}
	}
	
	public void init() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		MListView mListView=(MListView)page.getAttribute(KEY_MLISTVIEW);
		
		TreePath treePath=(TreePath)page.getAttribute(KEY_TREEPATH);
		page.setAttribute(KEY_TREEPATH, treePath);
		
		List<IDataBinding> fieldList =page.getAttribute(KEY_MLISTVIEW_FIELD_LIST);
		
		DataSourceConfig dataSourceConfig = page.getDataSourceConfigList().get(0);
		yunContext.setMasterModel(dataSourceConfig.getTableName());
		
		String action = yunContext.getQueryMap().get("action");
		Query query = new Query();
        if("dataView".equals(action)) {
        	Long id=Long.parseLong(yunContext.getQueryMap().get("id"));
        	List<ValueNamePair> pairList=new ArrayList<ValueNamePair>();
        	getTreePathValue(dataSourceConfig,id,pairList);
        	yunContext.setAttribute(treePath.getId()+"_value", pairList);
        	
        	query = new Query();
			query.add(Restrictions.eq("parent_id", id));
			
			dataSourceUtil.fillQuery(yunContext, query, dataSourceConfig);
			
			Table table=tableStore.get(dataSourceConfig.getTableName());
			IDataSource dataSource=dataSourceStore.getDataSource(table.getAccessType());
			TableData tableData=dataSource.select(yunContext, table, query);
			pageBeanUtil.generateMListViewInfo(mListView, fieldList, tableData);
		}else {
			query.add(Restrictions.isNull("parent_id"));
			Table table=tableStore.get(dataSourceConfig.getTableName());
			IDataSource dataSource=dataSourceStore.getDataSource(table.getAccessType());
			TableData tableData=dataSource.select(yunContext, table, query);
			if(tableData.getData().size()>0) {
				Map<String,Object> dataMap=tableData.getData().get(0);
				List<ValueNamePair> pairList=new ArrayList<ValueNamePair>();
				ValueNamePair pair=new ValueNamePair((String)dataMap.get("name"),dataMap.get("id"));
				pairList.add(pair);
				yunContext.setAttribute(treePath.getId()+"_value", pairList);
				
				query = new Query();
				query.add(Restrictions.eq("parent_id", pair.getValue()));
				
				dataSourceUtil.fillQuery(yunContext, query, dataSourceConfig);
				tableData=dataSource.select(yunContext, table, query);
				pageBeanUtil.generateMListViewInfo(mListView, fieldList, tableData);
			}
		}
	}
	
	public void getChildren() {
		YunContext yunContext = YunContext.getCurrentContext();
		Long idValue = Long.parseLong(yunContext.getViewRequest().getData().get("id"));
		getChildren(idValue);
	}
	
	public void getChildren(Long idValue) {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		TreePath treePath = (TreePath) page.getAttribute(KEY_TREEPATH);
		List<ValueNamePair> pairList = new ArrayList<ValueNamePair>();
		DataSourceConfig dataSourceConfig = page.getDataSourceConfigList().get(0);
		getTreePathValue(dataSourceConfig, idValue, pairList);
		yunContext.setAttribute(treePath.getId() + "_value", pairList);

		MListView mListView = (MListView) page.getAttribute(KEY_MLISTVIEW);
		List<IDataBinding> fieldList = page.getAttribute(KEY_MLISTVIEW_FIELD_LIST);
		Query query = new Query();
		query.add(Restrictions.eq("parent_id", idValue));
		
		dataSourceUtil.fillQuery(yunContext, query, dataSourceConfig);
		
		Table table=tableStore.get(dataSourceConfig.getTableName());
		IDataSource dataSource=dataSourceStore.getDataSource(table.getAccessType());
		TableData tableData=dataSource.select(yunContext, table, query);
		
		pageBeanUtil.generateMListViewInfo(mListView, fieldList,tableData);
	}
	
	public void onItemClickHandler() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		TreePath treePath = (TreePath) page.getAttribute(KEY_TREEPATH);
		Long idValue=variableService.getVariableValue(yunContext, treePath.getVarName());
		
		Query query = new Query();
		query.add(Restrictions.eq("id", idValue));
		DataSourceConfig dataSourceConfig = page.getDataSourceConfigList().get(0);
		Table table=tableStore.get(dataSourceConfig.getTableName());
		IDataSource dataSource=dataSourceStore.getDataSource(table.getAccessType());
		TableData tableData=dataSource.select(yunContext, table, query);
		if(tableData.getData().get(0).get("parent_id")==null) { //根节点
			MListView mListView = (MListView) page.getAttribute(KEY_MLISTVIEW);
			List<IDataBinding> fieldList =  page.getAttribute(KEY_MLISTVIEW_FIELD_LIST);
			yunContext.setAttribute(treePath.getId() + "_value", new ArrayList<>());
			pageBeanUtil.generateMListViewInfo(mListView, fieldList,tableData);
		}else {
			getChildren(idValue);
		}
		
	}
	
	protected void getTreePathValue(DataSourceConfig dataSourceConfig,Long idValue,List<ValueNamePair> pairList) {
		if(idValue==null) {
			return;
		}
		YunContext yunContext = YunContext.getCurrentContext();
		Query query = new Query();
		query.add(Restrictions.eq("id", idValue));
		Table table=tableStore.get(dataSourceConfig.getTableName());
		IDataSource dataSource=dataSourceStore.getDataSource(table.getAccessType());
		TableData tableData=dataSource.select(yunContext, table, query);
		Map<String,Object> dataMap=tableData.getData().get(0);
		ValueNamePair pair=new ValueNamePair((String)dataMap.get("name"),dataMap.get("id"));
		pairList.add(0, pair);
		
		Long parentId=(Long)dataMap.get("parent_id");
		getTreePathValue(dataSourceConfig,parentId,pairList);
	}
	
	public void dataEdit(String pageCode) {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page=yunContext.getPage();
		Long idValue = Long.parseLong(yunContext.getViewRequest().getData().get("id"));
		NavigatePageClientScript navigatePageClientScript = new NavigatePageClientScript();
		navigatePageClientScript.setPageId(yunContext.getPage().getId());
		navigatePageClientScript.setPageCode(pageCode);
		navigatePageClientScript.addPara("action", "dataEdit");
		navigatePageClientScript.addPara("id", idValue);
		
		TreePath treePath = (TreePath) page.getAttribute(KEY_TREEPATH);
		Long parentId=variableService.getVariableValue(yunContext, treePath.getVarName());
		if(parentId!=null) {
			navigatePageClientScript.addPara("parentId", parentId);
		}
		
		yunContext.addClientScript(navigatePageClientScript);
		yunContext.setRenderCompleted(true);
	}
	
	public void dataNew(String pageCode) {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page=yunContext.getPage();
		NavigatePageClientScript navigatePageClientScript = new NavigatePageClientScript();
		navigatePageClientScript.setPageId(yunContext.getPage().getId());
		navigatePageClientScript.setPageCode(pageCode);
		navigatePageClientScript.addPara("action", "dataNew");
		
		TreePath treePath = (TreePath) page.getAttribute(KEY_TREEPATH);
		Long parentId=variableService.getVariableValue(yunContext, treePath.getVarName());
		if(parentId!=null) {
			navigatePageClientScript.addPara("parentId", parentId);
		}
		yunContext.addClientScript(navigatePageClientScript);
		yunContext.setRenderCompleted(true);
	}
}
